package com.ship.dispatch.bpm.utils;

import java.io.IOException;

import java.io.StringWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Json 转换工具类
 *
 * @author Administrator
 */
public class JsonUtils {
    private static final ObjectMapper objectMapper = new ObjectMapper();
    private static final JsonFactory jsonFactory = new JsonFactory();

    public static <T> Map<String, T> jsonToMap(String jsonStr, Class<T> clazz) {
        Map<String, T> map = new HashMap<>();
        try {
            map = objectMapper.readValue(jsonStr, new TypeReference<Map<String, T>>() {
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
        return map;
    }

    public static <T> List<T> jsonToList(String jsonStr, Class<T> clazz) throws IOException {
        List<T> list = new ArrayList<>();
        try {
            list = objectMapper.readValue(jsonStr, new TypeReference<List<T>>() {
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    public static String mapToJson(Map<String, Object> map) {
        StringWriter sw = new StringWriter();
        JsonGenerator jg;
        try {
            jg = jsonFactory.createGenerator(sw);
            jg.useDefaultPrettyPrinter();
            objectMapper.writeValue(jg, map);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return sw.toString();
    }

    public static String objectToJson(Object obj) {
        StringWriter sw = new StringWriter();
        JsonGenerator jg;
        try {
            jg = jsonFactory.createGenerator(sw);
            jg.useDefaultPrettyPrinter();
            objectMapper.writeValue(jg, obj);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sw.toString();
    }

    public static <T> T jsonToObject(String json, Class<T> clazz) {
        T result = null;
        try {
            result = objectMapper.readValue(json, clazz);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }

    /**
     * Map转成实体对象
     * @param map map实体对象包含属性
     * @param clazz 实体对象类型
     * @return
     */
    public static  <T> T map2Object(Map<String, String[]> map, Class<T> clazz) {
        if (map == null) {
            return null;
        }
        T obj = null;
        try {
            obj = clazz.newInstance();

            Field[] fields = obj.getClass().getDeclaredFields();
            for (Field field : fields) {
                int mod = field.getModifiers();
                if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
                    continue;
                }
                if(field==null){
                    continue;
                }
                String[] value = map.get(field.getName());
                if(value==null || value[0]==null || "".equals(value[0])){
                    continue;
                }
                field.setAccessible(true);
                if(field.getType()==String.class){
                    field.set(obj, value[0]);
                }else if(field.getType()==Integer.class){
                    field.set(obj, Integer.parseInt(value[0]));
                }else if(field.getType()== Date.class){
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    field.set(obj, format.parse(value[0]));
                }else if(field.getType()== BigDecimal.class){
                    field.set(obj, new BigDecimal(value[0]));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }
    private static final char UNDERLINE = '_';

    public static String convertToCamelCase(String input) {
        StringBuilder result = new StringBuilder();

        // 使用split()方法将字符串分割成单词
        String[] words = input.split("_");

        for (String word : words) {
            // 首字母小写
            if (result.length() == 0) {
                result.append(word.toLowerCase());
            } else {
                // 首字母大写
                result.append(Character.toUpperCase(word.charAt(0)) + word.substring(1).toLowerCase());
            }
        }

        return result.toString();
    }

    public static String toCamelCase(String str) {
        if (str == null || str.isEmpty()) {
            return str;
        }
        return convertToCamelCase(str);
    }

    public static void main(String[] args) {
        String key = "test_yang_zhan";
        key = convertToCamelCase(key);
        System.out.println(key);
    }

    public static <T> T parseObject(String text, TypeReference<T> typeReference) {
        try {
            return objectMapper.readValue(text, typeReference);
        } catch (IOException e) {
            //log.error("json parse err,json:{}", text, e);
            throw new RuntimeException(e);
        }
    }
}